package de.deliancourt.jarvis.implementations;

import de.deliancourt.jarvis.interfaces.Existance;
import javax.media.j3d.*;
import javax.vecmath.*;

import com.sun.j3d.utils.geometry.*;
import com.sun.j3d.utils.universe.*;
import com.sun.j3d.utils.image.*;
import com.sun.j3d.utils.behaviors.vp.*;

public class lowlife implements Existance  {

	public lowlife() {
		Initialize();
	}
	
	public Canvas3D myCanvas3D;

	@Override
	public void WakeUp() {

	}

	@Override
	public void Initialize() {
		//Mechanism for closing the window and ending the program.
	    


	    //Default settings for the viewer parameters.
	    myCanvas3D = new Canvas3D(SimpleUniverse.getPreferredConfiguration());


	    //Construct the SimpleUniverse:
	    //First generate it using the Canvas.
	    SimpleUniverse simpUniv = new SimpleUniverse(myCanvas3D);


	    //Default position of the viewer.
	    simpUniv.getViewingPlatform().setNominalViewingTransform();


	    //The scene is generated in this method.
	    createSceneGraph(simpUniv);


	    //Add some light to the scene.
	    addLight(simpUniv);


	    //The following three lines enable navigation through the scene using the mouse.
	    OrbitBehavior ob = new OrbitBehavior(myCanvas3D);
	    ob.setSchedulingBounds(new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE));
	    simpUniv.getViewingPlatform().setViewPlatformBehavior(ob);


	    //Show the canvas/window.
	  

	}

	@Override
	public void Greeting() {
		// TODO Auto-generated method stub

	}

	@Override
	public void ShowUp() {
		// TODO Auto-generated method stub

	}
	 public void createSceneGraph(SimpleUniverse su)
	  {

	//*** A cube to be included in the scene including its transformation group.

	    //The colours for the Appearance.
	    Color3f ambientColourFBox = new Color3f(0.0f,0.0f,0.4f);
	    Color3f emissiveColourFBox = new Color3f(0.0f,0.0f,0.0f);
	    Color3f diffuseColourFBox = new Color3f(0.0f,0.0f,0.6f);
	    Color3f specularColourFBox = new Color3f(0.0f,0.0f,0.8f);
	    float shininessFBox = 10.0f;


	    //The Appearance for the cube.
	    Appearance fBoxApp = new Appearance();
	    fBoxApp.setMaterial(new Material(ambientColourFBox,emissiveColourFBox,
	                          diffuseColourFBox,specularColourFBox,shininessFBox));
	    
	    Texture img = new TextureLoader("src/de/deliancourt/jarvis/media/Alphamaps/test.png",null).getTexture();
	    ImageComponent2D myImage = (ImageComponent2D) img.getImage(0);
       Texture2D texture = new Texture2D();
       texture = new Texture2D(Texture2D.BASE_LEVEL, Texture2D.RGBA,512,512);
       texture.setImage(0, myImage);
       texture.setEnable(true);
         fBoxApp.setTexture(texture);
       TransparencyAttributes Trans = new TransparencyAttributes();
         Trans.setTransparencyMode (TransparencyAttributes.NICEST);
	   Trans.setTransparency(0.3f);
	    //fBoxApp.setTexture(textbox);
	    //fBoxApp.setTextureAttributes(texat);
	   TextureAttributes texAttr = new TextureAttributes();
	    texAttr.setTextureMode(TextureAttributes.MODULATE);
	   fBoxApp.setTextureAttributes(texAttr);
	   fBoxApp.setTransparencyAttributes(Trans);

	    //The cube.
      
       
	    Box fBox = new Box(0.2f,0.2f,0.005f,Box.GENERATE_TEXTURE_COORDS,fBoxApp);

	    //The transformation group of the cube.
	    Transform3D tfFBox = new Transform3D();
	    tfFBox.rotY(Math.PI/6);
	    Transform3D rotationX = new Transform3D();
	    rotationX.rotX(-Math.PI/5);
	    tfFBox.mul(rotationX);
	    TransformGroup tgFBox = new TransformGroup(tfFBox);
	    tgFBox.addChild(fBox);


	//*** And a sphere with its transformation group.

	    //The colours for the Appearance.
	    Color3f ambientColourBSphere = new Color3f(0.0f,0.4f,0.0f);
	    Color3f emissiveColourBSphere = new Color3f(0.0f,0.0f,0.0f);
	    Color3f diffuseColourBSphere = new Color3f(0.0f,0.0f,0.0f);
	    Color3f specularColourBSphere = new Color3f(0.0f,0.8f,0.0f);
	    float shininessBSphere = 128.0f;

	    //The Appearance for the sphere.
	    Appearance bSphereApp = new Appearance();
	    bSphereApp.setMaterial(new Material(ambientColourBSphere,emissiveColourBSphere,
	                          diffuseColourBSphere,specularColourBSphere,shininessBSphere));

	    //The sphere.
	    Sphere bSphere = new Sphere(0.4f,bSphereApp);

	    //The transformation group for the sphere.
	    Transform3D tfBSphere = new Transform3D();
	    tfBSphere.setTranslation(new Vector3f(0.0f,2.7f,-10.5f));
	    TransformGroup tgBSphere = new TransformGroup(tfBSphere);
	    tgBSphere.addChild(bSphere);


	    //Add everything to the scene.
	    BranchGroup theScene = new BranchGroup();
	    OrderedGroup Boxy = new OrderedGroup();
	    Boxy.addChild(tgFBox);
	    theScene.addChild(Boxy);
	    //theScene.addChild(tgBSphere);



	    //The bounding region for the background.
	    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0),Double.MAX_VALUE);
	    //Load the background image.
	    TextureLoader textureLoad = new TextureLoader("src/de/deliancourt/jarvis/media/textures/background.jpg",null);
	    //Define the image as the background and add it to the scene.
	    Background bgImage = new Background(textureLoad.getImage());
	    bgImage.setApplicationBounds(bounds);
	    theScene.addChild(bgImage);
	    theScene.compile();
	    su.addBranchGraph(theScene);


	    
	    //Add the scene to the universe.
	    

	  }






	  //Some light is added to the scene here.
	  public void addLight(SimpleUniverse su)
	  {

		
	    BranchGroup bgLight = new BranchGroup();

	    //Directional light.
	    BoundingSphere bounds = new BoundingSphere(new Point3d(0.0,0.0,0.0), Double.MAX_VALUE);
	    Color3f lightColour1 = new Color3f(1.0f,1.0f,1.0f);
	    Vector3f lightDir1  = new Vector3f(-1.0f,0.0f,-0.1f);
	    DirectionalLight light1 = new DirectionalLight(lightColour1, lightDir1);
	    light1.setInfluencingBounds(bounds);

	    bgLight.addChild(light1);


	    //Ambient light.
	    Color3f ambientLightColour = new Color3f(0.5f, 0.5f, 0.5f);
	    AmbientLight ambLight = new AmbientLight(ambientLightColour);
	    ambLight.setInfluencingBounds(bounds);
	    bgLight.addChild(ambLight);



	    su.addBranchGraph(bgLight);

	  }
}
